package jp.gr.java_conf.ryz.summarize.client.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.gr.java_conf.ryz.summarize.client.ObjectListBox;
import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecorationPair;
import jp.gr.java_conf.ryz.summarize.client.presenter.SummarizePresenter;
import jp.gr.java_conf.ryz.summarize.library.Action;
import jp.gr.java_conf.ryz.summarize.shared.model.Board;
import jp.gr.java_conf.ryz.summarize.shared.model.Category;
import jp.gr.java_conf.ryz.summarize.shared.model.ThreadDetail;
import jp.gr.java_conf.ryz.summarize.shared.model.Thred;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;

public class SummarizeViewImpl extends Composite implements SummarizeView
{
//	/**
//	 * logger.
//	 */
//	private static final Logger _logger = Logger.getLogger(SummarizeViewImpl.class.getName());
	
	private SummarizePresenter _presenter;
	
	private Panel _rootPanel;
	private Panel _mainPanel;
	private Label _threadTitleLabel;
	private ObjectListBox<Category> _categoryList;
	private ObjectListBox<Board> _boardList;
	private ObjectListBox<Thred> _threadList;
	private ViewerPanel _viewerPanel;
	
	private Panel _toolPanel;
	
	private LoadingPanel _loadingPanel;
	
	private Map<String, List<Board>> _categoryBoardMap = new HashMap<String, List<Board>>();
	
	
	public SummarizeViewImpl()
	{
		super();
		
		init();
	}
	
	@Override
	public void setPresenter(SummarizePresenter presenter)
	{
		_presenter = presenter;
		_presenter.initCategories();
	}
	
	@Override
	public void showLoadingPanel() {
		_loadingPanel.show();
	}

	@Override
	public void hideLoadingPanel() {
		_loadingPanel.hide();
	}

	@Override
	public void setCategories(List<Category> categories)
	{
		initializeSelectBoardPanel(categories);
	}
	
	@Override
	public Board getSelectedBoard()
	{
		return _boardList.getSelectedItem();
	}

	@Override
	public void setThreads(List<Thred> threads)
	{
		Board board = _boardList.getSelectedItem();
		if (board == null)
		{
			_threadList.clear();
			return;
		}
		
		_threadList.clear();
		for (Thred each : threads)
		{
			_threadList.addItem(each.getName(), each);
		}
	}
	
	@Override
	public Thred getSelectedThread()
	{
		return _threadList.getSelectedItem();
	}

	@Override
	public void setThreadDetail(Thred thread, ThreadDetail detail)
	{
		_threadTitleLabel.setText(thread.getName());
		_viewerPanel.setPosts(detail.getPosts());
	}
	
	@Override
	public void openExportDialog()
	{
		List<PostDecorationPair> selectedPairs = _viewerPanel.getSelectedPostDecorationPairs();
		
		PopupPanel panel = new ExportPanel(selectedPairs);
		panel.show();
	}
	
	@Override
	public void selectAllPost() {
		_viewerPanel.selectAllPanel();
	}

	@Override
	public void clearSelectAllPost() {
		_viewerPanel.clearSelectAllPanel();
	}

	@Override
	public void enableAllBold() {
		_viewerPanel.enableAllBold();
	}

	@Override
	public void disableAllBold() {
		_viewerPanel.disableAllBold();
	}
	
	
	private void init()
	{
		_loadingPanel = new LoadingPanel();
		_loadingPanel.hide();
		
		initializeRootPanel();
		initWidget(_rootPanel);
	}

	private void initializeRootPanel()
	{
		// initialize main panel
		_mainPanel = new FlowPanel();
		
		_mainPanel.add(new HTML("<h3>取得したいスレッドを選択してください</h3>"));
		
		Panel selectBoardPanel = new FlowPanel();
		selectBoardPanel.add(new InlineLabel("カテゴリー："));
		
		_categoryList = new ObjectListBox<Category>();
		_categoryList.setWidth("150px");
		selectBoardPanel.add(_categoryList);
		
		selectBoardPanel.add(new InlineLabel("板："));
		
		_boardList = new ObjectListBox<Board>();
		_boardList.setWidth("150px");
		
		selectBoardPanel.add(_boardList);
		_mainPanel.add(selectBoardPanel);
		
		_threadList = new ObjectListBox<Thred>();
		_threadList.setWidth("700px");
		_threadList.setVisibleItemCount(15);
		_mainPanel.add(_threadList);
		
		_mainPanel.add(new HTMLPanel("<hr />"));
		
		_threadTitleLabel = new Label();
		_threadTitleLabel.setStyleName("threadTitleLabel");
		_mainPanel.add(_threadTitleLabel);
		
		_viewerPanel = new ViewerPanel();
		_mainPanel.add(_viewerPanel);
		
		Action exportAction = new Action() {
			
			@Override
			public void invoke() {
				_presenter.openExportDialog();
			}
		};
		Action selectAllAction = new Action()
		{
			@Override
			public void invoke()
			{
				_presenter.selectAllPost();
			}
		};
		Action clearSelectAllAction = new Action()
		{
			@Override
			public void invoke()
			{
				_presenter.clearSelectAllPost();
			}
		};
		Action allBoldAction = new Action()
		{
			@Override
			public void invoke() {
				_presenter.enableAllBold();
			}
		};
		Action clearAllBoldAction = new Action()
		{
			@Override
			public void invoke() {
				_presenter.disableAllBold();
			}
		};
		_toolPanel = new ToolPanel(
				exportAction, selectAllAction, clearSelectAllAction, allBoldAction, clearAllBoldAction);
		
		_rootPanel = new HorizontalPanel();
		_rootPanel.add(_mainPanel);
		_rootPanel.add(_toolPanel);
	}
	
	private void initializeSelectBoardPanel(List<Category> categories)
	{
		_categoryList.addItem("カテゴリーを選択する", (Category)null);
		for (Category eachCategory : categories)
		{
			_categoryList.addItem(eachCategory.getName(), eachCategory);
			List<Board> boards = new ArrayList<Board>();
			for (Board eachBoard : eachCategory.getBoards())
			{
				boards.add(eachBoard);
			}
			_categoryBoardMap.put(eachCategory.getName(), boards);
		}
		
		_categoryList.addChangeHandler(new ChangeHandler()
		{
			
			@Override
			public void onChange(ChangeEvent event)
			{
				_boardList.clear();
				_boardList.addItem("板を選択する", (Board)null);
				
				Category category = _categoryList.getSelectedItem();
				if (category == null)
				{
					return;
				}
				
				for (Board each : _categoryBoardMap.get(category.getName()))
				{
					_boardList.addItem(each.getName(), each);
				}
			}
		});
		
		_boardList.addChangeHandler(new ChangeHandler()
		{
			
			@Override
			public void onChange(ChangeEvent event)
			{
				Board board = _boardList.getSelectedItem();
				if (board == null)
				{
					_threadList.clear();
					return;
				}
				
				_presenter.updateThreads();
			}
		});
		
		_threadList.addChangeHandler(new ChangeHandler()
		{
			
			@Override
			public void onChange(ChangeEvent event)
			{
				_presenter.updatePosts();
			}
		});
	}
}
